home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group00b.txt / 000130_icon-group-sender_Mon Oct 30 16:27:22 2000.msg < prev    next >
Internet Message Format  |  2001-01-03  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id e9UNRBu14167
  4.     for icon-group-addresses; Mon, 30 Oct 2000 16:27:12 -0700 (MST)
  5. Message-Id: <200010302327.e9UNRBu14167@baskerville.CS.Arizona.EDU>
  6. Date: Mon, 30 Oct 2000 14:31:48 -0700
  7. From: Steve Wampler <swampler@noao.edu>
  8. X-Accept-Language: en
  9. To: symbiot@my-deja.com
  10. CC: icon-group@cs.arizona.edu
  11. Subject: Re: How would the experts handle this...??
  12. Errors-To: icon-group-errors@cs.arizona.edu
  13. Status: RO
  14. Content-Length: 3009
  15.  
  16. symbiot@my-deja.com wrote:
  17. > Greetings! And welcome to my latest edition of "Questions from the
  18. > Newbie"
  19. > The situation is this:
  20. > I wish to scan thru a file and determine how letters "connect" with one
  21. > another. By that I mean that for every letter of the alphabet, I would
  22. > like to compile two lists: one containing all the letters which apper
  23. > to the right of the given letter, and another for all those appearing
  24. > to the left.
  25. ...
  26. > Thus far, I have been roundly criticized by icon afficiandos for my use
  27. > of explicit indexing. It seems to me an intuitive way to handle the
  28. > problems I've faced and makes the program "readable" to the layman.
  29.  
  30. "layman"?  As in someone who doesn't program?  I'd claim that *any* solution
  31. is unreadable in that case!  If you mean someone who knows C and not Icon,
  32. that may be right, but if that's your goal, then perhaps C would be a better
  33. choice to start with?
  34.  
  35. > But, be that as it may, I'm curious as to how those "in the know" would
  36. > handle this problem in a more "icon-esque" fashion.
  37.  
  38. Well, I took a few liberties:
  39.  
  40.    (1) I assume the output could just as well show lowercase versions of
  41.        the letters for the "middle" letter instead of uppercase versions.
  42.    (2) and I assume you really mean "letters" and not "characters" (your
  43. solution
  44.        also records non-letter characters on the left or right)
  45.    (3) and you don't want duplicated letters (your solution allows duplicated
  46.        letters, but I didn't read that as a requirement in the problem
  47.        statement)
  48.    (4) and, that the output should be formatted as shown in your
  49.        example (your code outputs it differently).
  50.    (5) the file name is given as a command line argument and all files named
  51.        on the command line should be processed and letter-pairs accumulated
  52.        across all the files (not file-by-file, which can be done by running
  53.        the program separately on each file).
  54.  
  55. Any of those is easy to change
  56.  
  57. Here's a solution:
  58.  
  59. global leftSides, rightSides
  60.  
  61. #
  62. # Accumulate letter-pair information for all lines of all files listed on
  63. #    the command line.
  64. #
  65. procedure main(args)
  66.    leftSides := table('')
  67.    rightSides := table('')
  68.  
  69.    every buildPairs(!open(!args))
  70.  
  71.    showResults()
  72. end
  73.  
  74. procedure buildPairs(s)
  75.     lChar := mChar := ''
  76.     every rChar := !s do {
  77.         # only remember pairs where left (or right) is a letter
  78.         #
  79.         leftSides[map(mChar)] ++:= (&letters ** lChar)
  80.         rightSides[map(mChar)] ++:= (&letters ** rChar)
  81.  
  82.         # shift characters to the left...
  83.         #
  84.         #   (aside: this also works!:  rChar :=: mChar :=: lChar)
  85.         #
  86.         lChar := mChar
  87.         mChar := rChar
  88.         }
  89. end
  90.  
  91. procedure showResults()
  92.     every c := !&lcase do {
  93.         # Could add test here to only print output that has left or right side
  94.         write(c,":\tleft-  ",leftSides[c])
  95.         write(   "\tright- ",rightSides[c])
  96.         }
  97. end
  98.  
  99.  
  100.  
  101. --
  102. Steve Wampler-  SOLIS Project, National Solar Observatory
  103. swampler@noao.edu
  104.